package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.entity.SysDept;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysClassificationEntity;
import com.company.project.service.SysClassificationService;



/**
 * 
 *
 * @author iukcy
 * @email iukcy2018@163.com
 * @date 2022-03-02 17:02:31
 */
@RestController
@RequestMapping("/")
public class SysClassificationController {
    @Autowired
    private SysClassificationService sysClassificationService;


    @ApiOperation(value = "新增")
    @PostMapping("sysClassification")
    @RequiresPermissions("sysClassification:add")
    public DataResult add(@RequestBody SysClassificationEntity sysClassification){
        sysClassificationService.save(sysClassification);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysClassification/{id}")
    @RequiresPermissions("sysClassification:delete")
    public DataResult delete(@PathVariable("id") String id){
        sysClassificationService.removeById(id);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysClassification")
    @RequiresPermissions("sysClassification:update")
    public DataResult update(@RequestBody SysClassificationEntity sysClassification){
        sysClassificationService.updateById(sysClassification);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysClassification/listByPage")
    @RequiresPermissions("sysClassification:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysClassificationEntity sysClassification){
        Page page = new Page(sysClassification.getPage(), sysClassification.getLimit());
        LambdaQueryWrapper<SysClassificationEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(SysClassificationEntity::getId, sysClassification.getId());
        IPage<SysClassificationEntity> iPage = sysClassificationService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @GetMapping("/sysClassification/tree")
    @ApiOperation(value = "树型项目分类列表接口")
    @LogAnnotation(title = "项目分类管理", action = "树型项目分类列表")
    @RequiresPermissions(value = {"sysClassification:list", "sysClassification:update", "sysClassification:add", "sysClassification:add", "sysClassification:update"}, logical = Logical.OR)
    public DataResult getTree(@RequestParam(required = false) String classId) {
        return DataResult.success(sysClassificationService.classTreeList(classId, false));
    }

    @GetMapping("/sysClassifications")
    @ApiOperation(value = "获取项目分类列表接口")
    @LogAnnotation(title = "项目分类管理", action = "获取所有项目分类")
    @RequiresPermissions("sysClassification:list")
    public DataResult getClassAll() {
        List<SysClassificationEntity> classList = sysClassificationService.list();
        classList.parallelStream().forEach(entity -> {
            SysClassificationEntity parentClass = sysClassificationService.getById(entity.getPId());
            if (parentClass != null) {
                entity.setPIdName(parentClass.getName());
            }
        });
        return DataResult.success(classList);
    }
}
